﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;

namespace PCDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            cbCom.Items.Clear();
            cbCom.Items.AddRange(ports);
            cbCom.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "开启服务")
            {
                MyCOM.PortName = cbCom.Text;
                try
                {
                    MyCOM.Open();
                    button1.Text = "停止服务";
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("指定串口无法打开");
                    button1.Text = "开启服务";
                }
            }
            else
            {
                MyCOM.Close();
                button1.Text = "开启服务";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MyCOM.DataReceived -= new SerialDataReceivedEventHandler(MyCOM_DataReceived);
            int sys = (int)numericUpDown1.Value;
            int i1, i2, i3, i4;
            byte h,l,tx;
            i1 = sys / 1000;
            sys = sys % 1000;
            i2 = sys / 100;
            sys = sys % 100;
            i3 = sys / 10;
            i4 = sys % 10;
            //设置
            byte[] Tx = new byte[8];
            Tx[0] = 0x16;
            Tx[1] = 0x16;            
            Tx[2] = 0xff;
            Tx[3] = 0x02;
            h=(byte)i1;
            l=(byte)i2;
            tx=(byte)(h<<4);
            tx=(byte)(tx+l);
            Tx[4] = tx;
            h = (byte)i3;
            l = (byte)i4;
            tx = (byte)(h << 4);
            tx = (byte)(tx + l);
            Tx[5] = tx;            
            Tx[6] = GetVerify_byteSum(Tx, 0,6);
            Tx[7] = 0x0d;
            byte needLen = (byte)(2 + 6);
            byte[] RxList = new byte[2 + 6];
            int retry = 100;
            byte RxCount = 0;
            try
            {
                MyCOM.Write(Tx, 0, 8);
                textBox2.Text = byteToHexStr(Tx," ");
                textBox1.Text = "";
                while (retry != 0)
                {
                    RxList[RxCount++] = (byte)MyCOM.ReadByte();
                    if (RxCount == 1)
                    {
                        if (RxList[0] != 0x16)
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (RxCount == 2)
                    {
                        if (RxList[1] != 0x16)
                        {
                            RxCount=0;
                            retry--;
                        }
                    }                   
                    else if (RxCount == 3)//命令
                    {
                        if (RxList[2] != 0xff)
                        {
                            RxCount=0;
                            retry--;
                        }
                    }
                    else if (RxCount == 4)//长度
                    {
                        if (needLen != (byte)(RxList[3] + 6))
                        {
                            RxCount=0;
                            retry--;
                        }
                    }
                    else if (RxCount > 4)//判断是否接收完成
                    {
                        if (RxCount == needLen && RxList[RxCount - 1] == 0x0d)//正确完成
                        {
                            if (GetVerify_byteSum(RxList, 0,RxCount - 2) == RxList[RxCount - 2])//校验正确
                            {
                                textBox1.Text = byteToHexStr(RxList, " ");
                                MessageBox.Show("设置成功");                                
                                break;
                            }
                            else
                            {
                                retry = 0;
                                break;
                            }
                        }
                        else
                        {
                            if (RxCount > needLen)
                            {
                                retry = 0;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                retry = 0;
            }
            if (retry == 0)
            {
                MessageBox.Show("设置失败");
            }
            MyCOM.DataReceived += new SerialDataReceivedEventHandler(MyCOM_DataReceived);
        }
        public byte GetVerify_byteSum(byte[] pBuff, int start, int count)
        {
            byte sum = 0;
            for (int i = 0; i < count; i++)
                sum = (byte)(sum + pBuff[i + start]);
            return sum;
        }
        public string byteToHexStr(byte[] bytes, string spilt)
        {
            StringBuilder strB = new StringBuilder();
            if (bytes != null)
            {
                foreach (byte b in bytes)
                {
                    strB.Append(b.ToString("X2"));
                    strB.Append(spilt);
                }
            }
            return strB.ToString();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            MyCOM.DataReceived -= new SerialDataReceivedEventHandler(MyCOM_DataReceived);
            //设置
            textBox4.Text = "";
            byte[] Tx = new byte[6];
            Tx[0] = 0x16;
            Tx[1] = 0x16;
            Tx[2] = 0xfe;
            Tx[3] = 0x00;            
            Tx[4] = GetVerify_byteSum(Tx, 0, 4);
            Tx[5] = 0x0d;
            byte needLen = (byte)(2 + 6);
            byte[] RxList = new byte[2 + 6];
            int retry = 100;
            byte RxCount = 0;
            try
            {
                MyCOM.Write(Tx, 0, 6);
                textBox2.Text = byteToHexStr(Tx, " ");
                textBox1.Text = "";
                while (retry != 0)
                {
                    RxList[RxCount++] = (byte)MyCOM.ReadByte();
                    if (RxCount == 1)
                    {
                        if (RxList[0] != 0x16)
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (RxCount == 2)
                    {
                        if (RxList[1] != 0x16)
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (RxCount == 3)//命令
                    {
                        if (RxList[2] != 0xfe)
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (RxCount == 4)//长度
                    {
                        if (needLen != (byte)(RxList[3] + 6))
                        {
                            RxCount = 0;
                            retry--;
                        }
                    }
                    else if (RxCount > 4)//判断是否接收完成
                    {
                        if (RxCount == needLen && RxList[RxCount - 1] == 0x0d)//正确完成
                        {
                            if (GetVerify_byteSum(RxList, 0, RxCount - 2) == RxList[RxCount - 2])//校验正确
                            {
                                textBox1.Text = byteToHexStr(RxList, " ");
                                int x = RxList[4] << 8;
                                x = x + RxList[5];
                                textBox4.Text = x.ToString("X4");
                                MessageBox.Show("查询成功");
                                break;
                            }
                            else
                            {
                                retry = 0;
                                break;
                            }
                        }
                        else
                        {
                            if (RxCount > needLen)
                            {
                                retry = 0;
                                break;
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                retry = 0;
            }
            if (retry == 0)
            {
                MessageBox.Show("查询失败");
            }
            MyCOM.DataReceived += new SerialDataReceivedEventHandler(MyCOM_DataReceived);
        }
        byte[] COMRx = new byte[11];
        int COMRxCount = 0;
        private void MyCOM_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int count = MyCOM.BytesToRead;
            while (count != 0)
            {
                byte rx = (byte)MyCOM.ReadByte();
                count--;
                COMRx[COMRxCount++] = rx;
                if (COMRxCount == 1)
                {
                    if (COMRx[0] != 0x16)
                    {
                        COMRxCount = 0;
                    }
                }
                else if (COMRxCount == 2)
                {
                    if (COMRx[1] != 0x16)
                    {
                        COMRxCount = 0;
                    }
                }
                else if (COMRxCount == 3)//命令
                {
                    if (COMRx[2] != 0x01 && COMRx[2]!=0x02 && COMRx[2]!=0x03)
                    {
                        COMRxCount = 0;
                    }
                }
                else if (COMRxCount == 4)//长度
                {
                    if (COMRx[3]!=5)
                    {
                        COMRxCount = 0;
                    }
                }
                else if (COMRxCount > 4)//判断是否接收完成
                {
                    if (COMRxCount == 11 && COMRx[10] == 0x0d)//正确完成
                    {
                        if (GetVerify_byteSum(COMRx, 0, 9) == COMRx[9])//校验正确
                        {
                            textBox1.Text = byteToHexStr(COMRx, " ");
                            byte[] Tx = new byte[7];
                            Tx[0] = 0x16;
                            Tx[1] = 0x16;
                            Tx[2] = COMRx[2];
                            Tx[3] = 0x01;
                            if (radioButton1.Checked)//成功
                                Tx[4] = 0x01;
                            else if (radioButton2.Checked)//重复
                                Tx[4] = 0x02;
                            else
                                Tx[4] = 0x03;//失败
                            Tx[5] = GetVerify_byteSum(Tx, 0, 5);
                            Tx[6] = 0x0d;
                            MyCOM.Write(Tx, 0, 7);
                            textBox2.Text = byteToHexStr(Tx, " ");                            
                        }
                        COMRxCount = 0;
                    }
                    else
                    {
                        if (COMRxCount > 10)
                        {
                            COMRxCount = 0;                            
                        }
                    }
                }
            }            
        }
    }
}
